{% macro contributor_list(contributors) -%}
  <section id="doc-contributors" class="clearfix">
    {% trans contributors=user_list(contributors), link=url('landings.get_involved') %}
      <span class="help-title-prefix">//</span>
      <span class="help-title">These fine people helped write this article:</span><span class="contributors">{{ contributors }}.</span>
      <span class="help-link cf">You can help too - <a href="{{ link }}">find out how</a>.</span>
    {% endtrans %}
  </section>
{%- endmacro %}

{% macro document_title(document) -%}
  <h1 class="title">{{ document.title }}</h1>
{%- endmacro %}

{% macro document_messages(document, redirected_from) -%}
  {% if redirected_from %}
    <div id="redirected-from">
      {{ _('(Redirected from <a href="{href}">{title}</a>)')|fe(href=redirected_from.get_absolute_url()|urlparams(redirect='no'), title=redirected_from.title) }}
    </div>
  {% endif %}
  {% if document.is_majorly_outdated() and not document.is_archived %}
    <div class="warning-box">
      {% trans url=document.parent.get_absolute_url(), title=document.parent.title %}
        <p>This article may be out of date.</p>
        <p>
          An important change has been made to the English article on which
          this is based. Until this page is updated, you might find this
          helpful: <a href="{{ url }}">{{ title }}</a>
        </p>
      {% endtrans %}
    </div>
  {% endif %}
  {% if document.is_archived %}
    <div class="warning-box">
        <p>
          {% trans %}
            This article is no longer maintained, so its content might be out
            of date.
          {% endtrans %}
        </p>
    </div>
  {% endif %}
{%- endmacro %}

{% macro document_content(document, fallback_reason, request, settings, css_class='', any_localizable_revision=False, full_locale_name=None) -%}
  <section id="doc-content" class="{{ css_class }}">
    {% if not fallback_reason %}
      {{ document.html|safe }}
    {% elif fallback_reason == 'no_translation' %}
      {% if document.is_localizable and any_localizable_revision and not document.is_archived %}
        <div id="doc-pending-fallback" class="warning-box">
          {% trans help_link=url('wiki.document', 'localize-firefox-help'), document_trans_link=url('wiki.translate', document.slug) %}
            No one has helped translate this article yet. If you already know how localizing for SUMO works,
            <a href="{{ document_trans_link }}">start translating now</a>.
            If you want to learn how to translate articles for SUMO,
            <a href="{{ help_link }}">please start here</a>.
          {% endtrans %}
        </div>
      {% endif %}
      {{ document.html|safe }}
    {% elif fallback_reason == 'translation_not_approved' %}
      <div id="doc-pending-fallback" class="warning-box">
        {# L10n: This is shown for existing, never-approved translations #}
        {% trans help_link=url('wiki.document', 'localize-firefox-help') %}
          Our volunteers are working on translating this article.
          Until it's ready, maybe the English version can be of some help.
          If you want to help us translate articles like this one,
          <a href="{{ help_link }}">please click here</a>.
        {% endtrans %}
      </div>
      {% if document.locale == settings.WIKI_DEFAULT_LANGUAGE %}
        {# This is the actual fallback document, show it. #}
        {{ document.html|safe }}
      {% else %}
        {{ document.parent.html|safe }}
      {% endif %}
    {% elif fallback_reason == 'fallback_locale' %}
      <div id="doc-pending-fallback" class="warning-box">
        {% trans locale=full_locale_name[request.LANGUAGE_CODE], fallback_locale=full_locale_name[document.locale], help_link=url('wiki.document', 'localize-firefox-help'), eng_link=document.parent.get_absolute_url() %}
          This page does not exist in {{ locale }}. You have been redirected to the {{ fallback_locale }} version instead.
          If you would like to localize it into {{ locale }}, <a href="{{ help_link }}">click here</a>.
          You can also see the <a href="{{ eng_link }}">English</a> version of this page.
        {% endtrans %}
      </div>
      {{ document.html|safe }}
    {% else %}
      {{ _("This article doesn't have approved content yet.") }}
    {% endif %}
  </section>
{%- endmacro %}

{% macro topic_list(topics) -%}
  {% set comma = joiner(', ') %}
  {% for topic in topics -%}
    {{ comma() }}
    <a href="{{ url('wiki.topic', topic.slug) }}">{{ _(topic.name) }}</a>
  {%- endfor %}
{%- endmacro %}

{% macro edit_messages(document, show_revision_warning) -%}
  {% if show_revision_warning %}
    <li class="warning">
      <h2>{{ _('Warning: There are newer unreviewed revisions of this article.') }}</h2>
      <p>
        {% trans url=url('wiki.document_revisions', document.slug) %}
          See <a href="{{url}}">the history</a> to base your edit on an
          existing unreviewed revision.
        {% endtrans %}
      </p>
    </li>
  {% endif %}
{%- endmacro %}

{% macro revision_diff(revision_from, revision_to, revision_from_header=None, revision_to_header=None, show_picker=False) -%}
  {% if revision_from and revision_to %}
    <div class="revision-diff">
      <header>
        {% if show_picker %}
          <div class="picker">
            <a href="{{ url('wiki.document_revisions', revision_from.document.slug)|urlparams(locale=revision_from.document.locale) }}">{{ _('Change Revisions') }}</a>
          </div>
        {% endif %}
        <div class="col">
          <h3>
            {% if revision_from_header %}
              {{ revision_from_header}}
            {% else %}
              <a href="{{ url('wiki.revision', revision_from.document.slug, revision_from.id) }}">
                {{ _('Revision {num}:')|f(num=revision_from.id) }}
              </a>
            {% endif %}
          </h3>
          <p>{{ _('Revision {id} by {user} on {date}')|fe(id=revision_from.id, user=revision_from.creator, date=datetimeformat(revision_from.created, format='longdatetime')) }}</p>
        </div>
        <div class="col">
          <h3>
            {% if revision_to_header %}
              {{ revision_to_header}}
            {% else %}
              <a href="{{ url('wiki.revision', revision_to.document.slug, revision_to.id) }}">
                {{ _('Revision {num}:')|f(num=revision_to.id) }}
              </a>
            {% endif %}
          </h3>
          <p>{{ _('Revision {id} by {user} on {date}')|fe(id=revision_to.id, user=revision_to.creator, date=datetimeformat(revision_to.created, format='longdatetime')) }}</p>
        </div>
      </header>
      <h4>{{ _('Keywords:') }}</h4>
      <div class="diff-this">
        <div class="from">{{ revision_from.keywords }}</div>
        <div class="to">{{ revision_to.keywords }}</div>
        <div class="output"></div>
      </div>
      <h4>{{ _('Search results summary:') }}</h4>
      <div class="diff-this">
        <div class="from">{{ revision_from.summary }}</div>
        <div class="to">{{ revision_to.summary }}</div>
        <div class="output"></div>
      </div>
      <h4>{{ _('Content:') }}</h4>
      <div id="content-diff" class="diff-this">
        <div class="from">{{ revision_from.content }}</div>
        <div class="to">{{ revision_to.content }}</div>
        <div class="output"></div>
      </div>
    </div>
  {% endif %}
{%- endmacro %}

{% macro document_watch(document, user) -%}
  {% if user.is_authenticated() and document %}
    <div id="doc-watch">
      {% if document.is_watched_by(user) %}
        <form action="{{ url('wiki.document_unwatch', document.slug) }}" method="post">
          {% csrf_token %}
          <input type="submit" class="link-btn" value="{{ _('Stop article emails') }}" />
        </form>
      {% else %}
        <form action="{{ url('wiki.document_watch', document.slug) }}" method="post">
          {% csrf_token %}
          <input type="submit" class="link-btn" value="{{ _('Email me article changes') }}" />
        </form>
      {% endif %}
    </div>
  {% endif %}
{%- endmacro %}

{% macro submit_revision(form, buttons_only=False, show_no_update_checkbox=False, include_diff=False, translate=False) -%}
  <div class="submit">
    <button class="btn btn-important btn-submit" type="submit">{{ _('Submit for Review') }}</button>
    {% if translate %}
      <button class="btn btn-important btn-draft" data-draft-url="{{ url('wiki.draft_revision') }}" type="button">{{ _('Save as Draft') }}</button>
    {% endif %}
    <button class="btn btn-preview" data-preview-url="{{ url('wiki.preview') }}" type="button">{{ _('Preview Content') }}</button>
    {% if include_diff %}
      <button class="btn btn-diff" type="button">{{ _('Preview Changes') }}</button>
    {% endif %}
  </div>
  {% if not buttons_only %}
    <div>{# Dummy wrapper so modal doesn't get shown by details/summary code #}
      <div id="submit-modal" class="kbox" title="{{ _('Submit Your Changes') }}" data-target=".btn-submit" data-modal="true">
        <p>
          {% trans %}You rock! We'll send an email to let you know if your changes get approved. You
          can follow the discussion about this article by clicking the Discussion tab.{% endtrans %}
        </p>
        <div class="comment">
          <label for="id_comment">{{ _('Briefly describe your changes:') }}</label>
          {{ form.comment|safe }}
        </div>
        {% if show_no_update_checkbox %}
          <div class="no-update">
            <label>
              <input type="checkbox" name="no-update" value="Yes" />
              {{ _('This edit does not make this article up to date. The English differences should not change on the next edit.') }}
            </label>
          </div>
        {% endif %}
        <div class="notifications">
          <label>
            <input type="checkbox" name="notify-future-changes" value="Yes" />
            {{ _('Notify me of any future changes to this page') }}
          </label>
        </div>
        <button class="btn btn-important" type="submit">{{ _('Submit') }}</button>
        <a class="kbox-cancel" href="#cancel">{{ _('Cancel') }}</a>
      </div>
    </div>
  {% endif %}
{%- endmacro %}

{% macro show_for(products, header=None, close_button=False) -%}
  {% if header == None %}
    {% set header = _('Help with:') %}
  {% endif %}

  <section class="showfor" id="showfor-panel">
    <script type="application/json" class="showfor-data">
      {{ showfor_data(products) }}
    </script>

    {{ _('Customize this article') }}
    {% if header %}
      <h1>
        {{ header }}
        {% if close_button %}
          <div class="exit close-button" data-close-id="showfor-panel">x</div>
        {% endif %}
      </h1>
    {% endif %}

    {% for prod in products %}
      {% if prod.visible %}
        {% set has_versions = prod.versions.count() > 1 %}
        {% set has_platforms = prod.platforms.count() > 1 %}

        <div class="product" data-product="{{ prod.slug }}">
          <h2>
            <input type="checkbox" checked value="product:{{ prod.slug }}">
            {{ prod.title }}
          </h2>

          {% if has_versions %}
            <div class="selectbox-wrapper">
              <select class="version">
                {% for version in prod.versions.all() %}
                  {% if version.visible %}
                    <option value="version:{{ version.slug }}"
                            data-min="{{ version.min_version }}"
                            data-max="{{ version.max_version }}"
                            {% if version.default %}selected{% endif %}>
                      {{ version.name }}
                    </option>
                  {% endif %}
                {% endfor %}
              </select>
            </div>
          {% endif %}

          {% if has_platforms %}
            <div class="selectbox-wrapper">
              <select class="platform">
                {% for platform in prod.platforms.all() %}
                  {% if platform.visible %}
                    <option value="platform:{{ platform.slug }}" {% if platform.default %}selected{% endif %}>
                      {{ platform.name }}
                    </option>
                  {% endif %}
                {% endfor %}
              </select>
            </div>
          {% endif %}

        </div>
      {% endif %}
    {% endfor %}

  </section>

{%- endmacro %}

{% macro vote_form(document, source=None) -%}
  {% if not header %}
    {% set header = _('Was this article helpful?') %}
  {% endif %}
  {% if document.allows_vote(request) %}
    <div class="document-vote">
      <form class="helpful" action="{{ url('wiki.document_vote', document_slug=document.slug) }}" method="post">
        <p>
          <span class="header">{{ header }}</span>
          <input type="hidden" name="revision_id" value="{{ document.current_revision.id }}" />
          {% if source %}
            <input type="hidden" name="source" value="{{ source }}" />
          {% endif %}
          <input type="submit" class="btn" name="helpful" value="{{ _('Yes') }}" />
          <input type="submit" class="btn" name="not-helpful" value="{{ _('No') }}" />
          <img class="wait" src="{{ settings.STATIC_URL }}sumo/img/customercare/spinner.gif" alt="{{ _('Please wait...') }}" />
        </p>
      </form>
    </div>
  {% endif %}
{%- endmacro %}

{% macro revision_contributors_label(contributors) -%}
  {# This macro is used in the reject and approve revision modals #}
  {% if contributors|length == 1 %}
    {{ _('Leave a message for {contributor}: ')|fe(contributor=contributors[0]) }}
  {% else %}
    {{ _('Leave a message for {rest} and {last}:')|fe(rest=', '.join(contributors[:-1]), last=contributors[-1]) }}
  {% endif %}
{%- endmacro %}

{% macro document_lock_warning() -%}
  {% if locked %}
    <li id="locked-warning" class="warning">
      <h2>{% trans user=locked_by.username %}
        Warning: This page is also being edited by {{ user }}!
      {% endtrans %}</h2>
      <p>{{ _('If you know what you are doing, you can edit the document anyway.') }}</p>

      <a href="#" id="unlock-button" class="btn btn-warning">{{ _('Edit anyway') }}</a>
    </li>
  {% endif %}
{%- endmacro %}
